package controllers

import (
	"portstoragechanges/models"
	"portstoragechanges/pkg/app"
	"portstoragechanges/pkg/e"
	"portstoragechanges/services"

	"github.com/gin-gonic/gin"
)

func Getport(c *gin.Context) {
	err, data := services.Getport()
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
	}
	app.OK(c, gin.H{"value": data}, "查询成功")
}

func Delport(c *gin.Context) {
	data := c.Query("carid")
	if data == "-1" {
		app.INFO(c, 30001, "参数错误")
		return
	}
	err := services.Delport(data)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, gin.H{"value": data}, "删除成功")
}

func Updateport(c *gin.Context) {
	var data models.Port
	err := c.ShouldBindJSON(&data)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	err = services.Updateport(data)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, gin.H{"value": data}, "更新成功！")
}

func Addport(c *gin.Context) {
	var data models.Port
	err := c.ShouldBindJSON(&data)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	err = services.Addport(data)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, gin.H{}, "添加成功！")
}
